package com.bjwl.service.safty.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjwl.base.CurrUser;
import com.bjwl.dao.safty.LoginDao;
import com.bjwl.dao.safty.RoleDao;
import com.bjwl.exception.LoginException;
import com.bjwl.exception.SysException;
import com.bjwl.model.User;
import com.bjwl.service.safty.LoginService;
import com.bjwl.view.UserView;

/**
 * 业务层进行判断抛出自定义异常
 * @author Administrator
 *
 */
@Service
public class LoginServiceImpl implements LoginService {
	@Autowired
	private LoginDao loginDao;
	@Autowired
	private RoleDao roleDao;
	@Override
	public CurrUser login(UserView userView) {
		User user=null;
		String roleName="123";
		try {
		user =loginDao.login(userView);
		//获得当前用户的角色id
		Integer ro_id=roleDao.getRoleIdByUserId(userView.getU_id());
		//获取角色名
		roleName=roleDao.getRoleByRoleId(ro_id);
		System.out.println(roleName);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new SysException("系统错误");
		}
		if (user==null) {
			throw new LoginException("用户名或密码错误");
		}
		return new CurrUser(user.getU_id(),user.getU_name(),roleName);
	}
	
	public static void main(String[] args) {
		LoginService lo=new LoginServiceImpl();
		UserView userView=new UserView();
		userView.setU_id("admin");
		userView.setU_pwd("admin");
		System.out.println(userView);
		System.out.println(lo.login(userView));
	}

}
